package com.itheima.core.web.controller;
import javax.servlet.http.HttpSession;

import com.itheima.common.utils.Page;
import com.itheima.core.po.BaseDict;
import com.itheima.core.po.Customer;
import com.itheima.core.po.Role;
import com.itheima.core.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.itheima.core.po.User;
import com.itheima.core.service.UserService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器类
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String usercode,String password, Model model, 
                                                          HttpSession session) {
		// 通过账号和密码查询用户
		User user = userService.findUser(usercode, password);
		if(user != null){		
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			// 跳转到主页面
//			return "customer";
			return "index";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
         // 返回到登录页面
		return "login";
	}
	
	/**
	 * 模拟其他类中跳转到客户管理页面的方法
	 */
	@RequestMapping(value = "/toCustomer.action")
	public String toCustomer() {
	    return "customer";
	}
	
	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
	    // 清除Session
	    session.invalidate();
	    // 重定向到登录页面的跳转方法
	    return "redirect:login.action";
	}
	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
	    return "login";
	}

	@RequestMapping(value = "/user/list.action")
	public String list(Model model) {
		List<User> users = userService.findAllUser();
		List<Role> allRole = roleService.findAllRole();
		// 添加参数
		model.addAttribute("users", users);
		model.addAttribute("roles", allRole);
		return "user";
	}

/*	@RequestMapping(value = "/user/list.action")
	@ResponseBody
	public HashMap list(Model model) {
		HashMap map = new HashMap();
		List<User> users = userService.findAllUser();
		// 添加参数
		map.put("users",users);
		model.addAttribute("users", users);
		return map;
	}*/

	@RequestMapping(value = "/user/doUpdateUser.action")
	@ResponseBody
	public String doUpdateUser(User user){
		Integer integer = userService.updateUser(user);
		if (integer>0){
			return "OK";
		}else {
			return "Fail";
		}

	}
	@RequestMapping("/user/getUserById.action")
	@ResponseBody
	public HashMap<String,Object> getUserById(Integer id){
		HashMap<String,Object> map = new HashMap<>();
		User user = userService.findUserById(id);

		if (user!=null){
			map.put("code",0);
			map.put("msg","查询成功");
			map.put("user",user);


		}else {
			map.put("code",500);
			map.put("msg","查询失败");
		}

		return map;
	}
}
